<template>
  <div class="level-list">
    <el-tabs v-model="activeName">
      <el-tab-pane label="基础信息" name="first">
        <levelForm :formInfo="formInfo" :detail="detail" :levelType="levelType" :disabled="issueState > 0" :is-model="true" @saveData="saveData"></levelForm>
      </el-tab-pane>
      <el-tab-pane label="质控点信息" name="second">
        <!-- 搜索 -->
        <listSearch @search="search" @showTemplateDialog="showTemplateDialog" :levelType="levelType">
          <template slot="handle">
            <el-button type="primary" plain v-if="issueState==0 && detail != 'true'" @click="handle('add', formInfo)">添加质控点</el-button>
            <el-button type="primary" plain v-if="issueState==0 && detail != 'true'" @click="handle('quote')">引用</el-button>
          </template>
        </listSearch>
        <!-- 表格-已发布不允许修改和删除 -->
        <!-- 表格 -->
        <myTable :column="column" :tableData="tableData" :total="total" @changePage="changePage">
          <el-table-column label="操作" width="150px">
            <template slot-scope="scope">
              <el-button type="text" @click="handle('detail', scope.row, formInfo)">详情</el-button>
              <el-button type="text" v-if="issueState==0 && detail != 'true'" @click="handle('edit', scope.row, formInfo)">编辑</el-button>
              <el-button type="text" v-if="issueState==0 && detail != 'true'" @click="handle('del', scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </myTable>
      </el-tab-pane>
    </el-tabs>
    <div class="back-btn">
      <back-button></back-button>
    </div>

    <!-- 添加质控点 -->
    <addPoint v-if="addParams.visible" :addParams="addParams" :point-form="pointForm" @addOrEdit="addOrEdit"></addPoint>
    <!-- 引用质控点 -->
    <quotePoint v-if="quoteParams.visible" :quoteParams="quoteParams" @getList="getList"></quotePoint>
    <!-- 批量导入质控点 -->
    <addTemplatePoint @addTemplateSuccess="addTemplateSuccess" :targetStandardId="searchFrom.targetStandardId" :addTemplatePoint="addTemplatePoint"></addTemplatePoint>
  </div>
</template>

<script>
import { updateFormWorkData, getPointList, delPointData, addPointData, editPointData, getPointDetail, getFormWorkById } from '../../../api/target'
import { filterParams, getStorage } from '@/config/utils'
import listSearch from '@/components/search/listSearch'
import myTable from '@/components/table/index.vue'
import levelForm from './module/levelFrom'
import addPoint from '@/views/targetSystem/targetManage/module/addPoint'
import quotePoint from '@/views/targetSystem/targetManage/module/quotePoint'
import addTemplatePoint from '@/views/targetSystem/targetManage/module/addTemplatePoint.vue'

export default {
  name: 'levelList',
  components: {
    listSearch,
    levelForm,
    addPoint,
    quotePoint,
    myTable,
    addTemplatePoint
  },
  data() {
    return {
      activeName: 'first',
      searchFrom: {
        targetStandardId: '',
        indexOne: '',
        indexTwo: '',
        indexThree: '',
        name: '',
        pageSize: 10,
        pageIndex: 1
      },
      column: [
        {
          prop: 'indexOne',
          label: '一级指标'
        },
        {
          prop: 'indexTwo',
          label: '二级指标'
        },
        {
          prop: 'indexThree',
          label: '三级指标'
        },
        {
          prop: 'name',
          label: '质控点'
        },
        {
          prop: 'intension',
          label: '质控点内涵'
        },
        {
          prop: 'diagnosisStandard',
          label: '诊断标准'
        },
        {
          prop: 'belongPartName',
          label: '责任部门'
        },
        {
          prop: 'valueType',
          label: '值类型'
        },
        {
          prop: 'expectedValue',
          label: '预期值'
        },
        {
          prop: 'standardState',
          label: '审核状态',
          formatter: (row, column, cellValue, index) => {
            return cellValue == 1 ? '已审核' : '未审核'
          }
        },
        {
          prop: 'isWarning',
          label: '是否预警',
          asyncData: this.$config.yesOrNo
        },
        {
          prop: 'isOptional',
          label: '是否可选',
          asyncData: this.$config.isOptional
        }
      ],
      tableData: [],
      total: 0,
      addParams: {
        visible: false,
        title: '添加质控点',
        type: '',
        id: '',
        joinOrgDTOList: [],
        targetStandardId: ''
      },
      formInfo: null,
      pointForm: {},
      issueState: 0, // 下发状态
      isEdit: true,
      isDel: true,
      levelType: '',
      quoteParams: {
        visible: false,
        title: '引用质控点',
        type: '',
        id: '',
        targetStandardId: ''
      },
      userInfo: getStorage('userInfo'),
      detail: 0,
      // 是否显示批量导入弹框
      addTemplatePoint: {
        visible: false
      }
    }
  },
  created() {
    this.searchFrom.targetStandardId = this.$route.query.id
    this.searchFrom.rectificationRangeId = this.$route.query.rectificationRangeId
    this.detail = this.$route.query.detail
    this.searchFrom.levelType = this.$route.query.levelType
    this.issueState = this.$route.query.issueState || 0
    this.levelType = this.$route.params.type
    this.getList()
    this.getFormWorkById()
  },
  methods: {
    // 添加质控点
    handle(type, data, fromInfo) {
      switch (type) {
        case 'add':
          this.addParams = {
            visible: true,
            title: '添加质控点',
            type: type,
            joinOrgDTOList: data.joinOrgDTOList,
            targetStandardId: this.searchFrom.targetStandardId,
            optional: true
          }
          break
        case 'detail':
          this.addParams = {
            visible: true,
            title: '查看质控点',
            type: type,
            id: data.id,
            data: data,
            joinOrgDTOList: fromInfo.joinOrgDTOList,
            targetStandardId: this.searchFrom.targetStandardId,
            optional: true
          }
          this.getDetail()
          break
        case 'edit':
          this.addParams = {
            visible: true,
            title: '修改质控点',
            type: type,
            id: data.id,
            data: data,
            joinOrgDTOList: fromInfo.joinOrgDTOList,
            targetStandardId: this.searchFrom.targetStandardId,
            optional: true
          }
          this.getDetail()
          break
        case 'del':
          this.$confirm('您确定要删除这条数据吗?', '删除', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              delPointData(data.id)
                .then(res => {
                  this.$message.success('操作成功')
                  this.getList()
                })
                .catch(() => {})
            })
            .catch()
          break
        case 'quote':
          this.quoteParams = {
            visible: true,
            title: '引用质控点',
            type: type,
            targetStandardId: this.searchFrom.targetStandardId,
            rectificationRangeId: this.searchFrom.rectificationRangeId,
            levelType: this.searchFrom.levelType
          }
          break
      }
    },
    // 修改基础信息
    saveData(data) {
      updateFormWorkData(this.searchFrom.targetStandardId, data)
        .then(res => {
          this.$message.success('操作成功')
        })
        .catch()
    },
    // 查询质控点列表
    getList() {
      const params = filterParams(this.searchFrom)
      getPointList(params)
        .then(res => {
          this.tableData = res.dataList || []
          this.total = res.total
        })
        .catch(() => {})
    },
    // 搜索
    search(searchFrom) {
      this.searchFrom.pageIndex = 1
      this.searchFrom.indexOne = searchFrom.indexOne
      this.searchFrom.indexTwo = searchFrom.indexTwo
      this.searchFrom.indexThree = searchFrom.indexThree
      this.searchFrom.name = searchFrom.name
      this.searchFrom.belongPart = searchFrom.belongPart
      this.getList()
    },
    // 查询目标标准详情
    getFormWorkById() {
      if (this.searchFrom.targetStandardId) {
        getFormWorkById(this.searchFrom.targetStandardId)
          .then(res => {
            this.formInfo = res || {}
          })
          .catch(() => {})
      }
    },
    // 切换分页
    changePage(searchForm) {
      this.searchFrom.pageIndex = searchForm.pageIndex
      this.searchFrom.pageSize = searchForm.pageSize
      this.getList()
    },
    // 获取质控点模板详情
    getDetail() {
      getPointDetail(this.addParams.id)
        .then(res => {
          if (res) {
            this.pointForm = { ...res }
          }
        })
        .catch(() => {})
    },
    // 编辑或新增质控点
    addOrEdit(type, params) {
      switch (type) {
        case 'add':
          addPointData(params)
            .then(res => {
              this.$message.success('操作成功')
              this.addParams.visible = false
              this.getList()
            })
            .catch(() => {})
          break
        case 'edit':
          editPointData(this.addParams.id, params)
            .then(res => {
              this.$message.success('操作成功')
              this.addParams.visible = false
              this.getList()
            })
            .catch(() => {})
          break
      }
    },
    // 显示批量导入质控点
    showTemplateDialog() {
      this.addTemplatePoint.visible = true
    },
    // 批量导入成功
    addTemplateSuccess() {
      this.getList()
    }
  },
  watch: {
    $route: {
      handler(val) {
        // 如果index为2,默认打开second的tab页，默认显示质控点信息
        if (val.query.index === 2) {
          this.activeName = 'second'
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style lang="scss">
</style>
